﻿using ChurchDiary.DAL.Contract;
using ChurchDiary.DAL.Methods;
using ChurchDiary.TableEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace ChurchDiary.BLL
{
    public class BLL_EmailTemplate
    {
        #region EmailTemplate
        public EntityEmailTemplate GetTemplate(string TemplateType)
        {
            IEmailTemplate Etemplate = new DAL_EmailTemplate();
            return Etemplate.GetEmailTemplate(TemplateType);
        }
        public string GetEmailContent(EntityEmailTemplate ETemplate,Guid? Userid)
        {
            FullEntityEmailTemplate TemplateDetails = new FullEntityEmailTemplate();
            TemplateDetails.OrganizationInfo = new BLL_Organization().GetOrganizationInfo();
            TemplateDetails.SoftwareInfo = new BLL_Organization().GetSoftwareInfo();
            if (Userid != null)
            {
                TemplateDetails.User = new BLL_Organization().GetUserInfo(Userid);
            }
            string EmailContent = "";
            if (ETemplate != null)
            {
                EmailContent = ETemplate.TemplateContent;
                EmailContent = EmailContent.Replace("[Email Subject]", ETemplate.TemplateSubject);
                
                if (TemplateDetails.User != null)
                {
                    //For User Info
                    string Link = HttpContext.Current.Request.Url.Authority ;
                    EmailContent = EmailContent.Replace("[NAME]", TemplateDetails.User.Name);
                    EmailContent = EmailContent.Replace("[USERNAME]", TemplateDetails.User.UserName);
                    EmailContent = EmailContent.Replace("[PASSWORD]", TemplateDetails.User.Password);
                    EmailContent = EmailContent.Replace("[Link]", Link);
                }
                if (TemplateDetails.OrganizationInfo != null)
                {
                    //For Organization Info
                    string Logo = HttpContext.Current.Request.Url.Authority + "/Uploads/OrganizationFiles/" + TemplateDetails.OrganizationInfo.OrganizationLogo;
                    TemplateDetails.OrganizationInfo.OrganizationLogo = Logo;
                    EmailContent = EmailContent.Replace("[OrganizationName]", TemplateDetails.OrganizationInfo.NameOfOrganization);
                    EmailContent = EmailContent.Replace("[WEBSITE]", TemplateDetails.OrganizationInfo.OrganizationWebsite);
                    EmailContent = EmailContent.Replace("[OrganizationLogo]", TemplateDetails.OrganizationInfo.OrganizationLogo);
                }
                if (TemplateDetails.SoftwareInfo != null)
                {
                    //For Software Info
                    string Logo = HttpContext.Current.Request.Url.Authority + "/Uploads/OrganizationFiles/" + TemplateDetails.SoftwareInfo.SoftwareLogo;
                    TemplateDetails.SoftwareInfo.SoftwareLogo = Logo;
                    EmailContent = EmailContent.Replace("[SoftwareName]", TemplateDetails.SoftwareInfo.SoftwareName);
                    EmailContent = EmailContent.Replace("[SoftwareLogo]", TemplateDetails.SoftwareInfo.SoftwareLogo);
                    EmailContent = EmailContent.Replace("[SupportLink]", TemplateDetails.SoftwareInfo.SoftwareSupportLink);
                    EmailContent = EmailContent.Replace("[SuggestionLink]", TemplateDetails.SoftwareInfo.SoftwareSupportLink);
                }
            }
            return EmailContent;
        }
        #endregion
    }
}
